package com.sanjiahantang.youjiaclient.module.login.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.login.beans.LoginBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.beans.UserBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.login.views.LoginView;
import com.sanjiahantang.youjiaclient.module.login.views.impl.ForgetActivity;
import com.sanjiahantang.youjiaclient.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiaclient.module.login.views.impl.RegisterActivity;
import com.sanjiahantang.youjiaclient.module.login.views.impl.RegisterSuccessActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zy on 2017/9/4.
 */

public class LoginPresenter extends BaseMvpPresenter<LoginView> implements CallBack<UserBean> {
    private IUserModel userModel;

    public LoginPresenter(){
        userModel = new UserModel();
    }


    @Override
    public void onSuccess(UserBean response, Context context,Object tag) {
        hideProgress();
        if (response.getCode() == RESPONSE_CODE_SUC) {
//            String num = ConfigValue.userInfo.getCart_num() == null?"0":ConfigValue.userInfo.getCart_num();
            Intent mIntent = new Intent(ConfigValue.ACTION_ALTER_CARTGOODS_NUMS);
            mIntent.putExtra("cartgoodsnum", "0");
            //发送广播
            context.sendBroadcast(mIntent);
            ((BaseMvpActivity) context).finish();

        }
    }

    @Override
    public void onFailure(String message) {
        hideProgress();
    }

    public void loadLogin(Context context, String name, String password,String tag) {
        Map<String,String> params = new HashMap<>();
        params.put("phone",name);
        params.put("password", password);
        params.put("type","0");
        mView.showProgress();
        userModel.doLogin(context,params,new LoginCallBack(),tag);
    }

    public void toRegister(BaseMvpActivity context) {
        Intent intent=new Intent(context,RegisterActivity.class);
        context.startActivity(intent);
        context.finish();
    }

    public void toForget(BaseMvpActivity context) {
        Intent intent=new Intent(context,ForgetActivity.class);
        context.startActivity(intent);
        context.finish();
    }

    /**
     * 获取验证码
     * @param context
     * @param name
     * @param s
     */
    public void setSendCodeView(Context context, String name, String s) {
        Map<String,String> params = new HashMap<>();
        params.put("phone",name);
        mView.showProgress();
        userModel.senMessageCode(context,params,new VerificationCodeCallBack());
    }

    /**
     * 获取验证码
     * @param context
     * @param name
     * @param s
     */
    public void getRegisterCustomerPassWordCode(Context context, String name, String s) {
        Map<String,String> params = new HashMap<>();
        params.put("phone",name);
        mView.showProgress();
        userModel.getRegisterCustomerPassWordCode(context,params,new VerificationCodeCallBack());
    }


    //注册
    public void submitData(Context context, String code, String name, String password, String s) {

        Map<String,String> params = new HashMap<>();
        params.put("phone",name);
        params.put("pwd", password);
        params.put("identifyingCode",code);
        mView.showProgress();
        userModel.doforgetPassword(context,params,new RegistCallBack());


    }

    public void findPassword(final Context context, final String phone, final String pwd, String identifyingCode){
        final Map<String,String> params = new HashMap<>();
        params.put("phone",phone);
        params.put("pwd", pwd);
        params.put("identifyingCode",identifyingCode);
        mView.showProgress();
        userModel.findPassword(context,params, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
//                    Map<String,String> param = new HashMap<String, String>();
//                    param.put("phone",phone);
//                    param.put("password", pwd);
                    loadLogin(context,phone,pwd,"2");
                }else {
                    Utils.showToast(context,response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context,message);
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }

    private class LoginCallBack implements CallBack<LoginBean>{

        @Override
        public void onSuccess(LoginBean response, Context context,Object tag) {
            hideProgress();
            if(response.getCode() == RESPONSE_CODE_SUC){
                //绑定tag
                Set<String> set = new HashSet<>();
                set.add("1");
                JPushInterface.setTags(context,0,set);

                String key = response.getData().getSession().getToken();
                SPUtils.put(context, SPConfig.KEY, key);
                if (response.getData().getSession().getUserSessionDto().getUserName() != null){
                    SPUtils.put(context, SPConfig.USERNAME, response.getData().getSession().getUserSessionDto().getUserName());
                }

                SPUtils.put(context, SPConfig.PHONE, response.getData().getSession().getUserSessionDto().getPhone());
                JPushInterface.setAlias(context,0,response.getData().getSession().getUserSessionDto().getPhone());
                if (JPushInterface.isPushStopped(context)){
                    JPushInterface.resumePush(context);
                }
                if (response.getData().getSession().getUrl() != null){
                    SPUtils.put(context, SPConfig.HEAD_URL, response.getData().getSession().getUrl());
                }




                ConfigValue.DATA_KEY = key;
                if (tag.equals("0")){
                    context.startActivity(new Intent(context, RegisterSuccessActivity.class));
                    ((Activity)context).finish();
                }else if(tag.equals("2")){
                    ((Activity)context).finish();//忘记密码
                }else {
                    mView.showProgress();
                    Map<String,String> param = new HashMap<>();
                    param.put(KEY_TOKEN,key);
                    param.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
                    userModel.dopersonData(context,param,LoginPresenter.this);
                }

            }else {
                Utils.showToast(context,response.getMessage() == null ? "登录失败" : response.getMessage());
            }
//            if (response.getCode().equals("1")) {
//                Map<String,String> params = new HashMap<>();
//                String key = response.getData().getKey();
//                SPUtils.put(context, SPConfig.KEY, key);
//                ConfigValue.DATA_KEY = key;
//                params.put("key", ConfigValue.DATA_KEY);
//                mView.showProgress();
//                userModel.loadUserInfo((BaseMvpActivity) context,params,LoginPresenter.this);
//            }
//            Utils.showToast(context, response.getMessage());
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }


    //注册回调
    private class RegistCallBack implements CallBack<StringBean>{
        @Override
        public void onSuccess(StringBean response, Context context,Object tag) {
            mView.hideProgress();
            if(response.getCode() == RESPONSE_CODE_SUC){
                Utils.showToast(context, response.getMessage());
                LoginActivity.logout(context);
            }
//            if (response.getCode().equals("1")) {aaaaaaaaaaaa
//                Map<String,String> params = new HashMap<>();
//                String key = response.getData().getKey();
//                SPUtils.put(context, SPConfig.KEY, key);
//                ConfigValue.DATA_KEY = key;
//                params.put("key", ConfigValue.DATA_KEY);
//                mView.showProgress();
//                userModel.loadUserInfo((BaseMvpActivity) context,params,LoginPresenter.this);
//            }

        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
    //验证码回调
    private class VerificationCodeCallBack implements CallBack<StringBean>{
        @Override
        public void onSuccess(StringBean response, Context context,Object tag) {
            mView.hideProgress();
            if(response.getCode() == RESPONSE_CODE_SUC){
                Utils.showToast(context, response.getMessage());
            }else {
                Utils.showToast(context, response.getMessage());
            }
        }
        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }
    }
}
